<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\UserLoginRequest;
use App\Http\Requests\UserRegistCodeRequest;
use App\Http\Requests\UserRegistRequest;
use App\Models\Config;
use App\Models\IntegralDetail;
use App\Models\User;
use App\Models\UserRegistCode;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;


class UserController extends Controller
{

    public function login(UserLoginRequest $request)
    {
        if($user = User::where([['phone',$request->get('phone')]])->first()){
            if(Hash::check($request->get('password'),$user->password)){
                $user->token = md5($request->get('phone').$request->get('password').uniqid());
                $user->save();
                return ['status_code'=>1,'token'=>$user->token];
            }else{
                return ['status_code'=>-1,'message'=>"密码不正确"];
            }
        }else{
            return ['status_code'=>-1,'message'=>"登陆信息错误"];
        }
    }

    public function user_info(Request $request)
    {
        $user = $request->get('user');
        $user->my_avatar;
        return response()->json(['status_code'=>'1','data'=>$user]);
    }
    public function not_found()
    {
        return ['status_code'=>-1,'message'=>"登陆信息错误"];
    }
    //注册
    public function regist(UserRegistRequest $request)
    {
        $flag = $this->auth_code($request->get('phone'),$request->get('code'));
        if($flag){
            $data = [
                'phone'=>$request->get('phone'),
                'password'=>bcrypt($request->get('password')),
                'share_time'=>date('Y-m-d H:i:s',time()),
                'token'=>md5($request->get('phone').$request->get('password').uniqid()),
            ];
            $user = User::register($data);
            return response()->json(['status_code'=>'1','token'=>$user->token]);
        }else{
            return response()->json(['status_code'=>'0','message'=>'验证码错误']);
        }
    }

    public function regist_code(UserRegistCodeRequest $request)
    {
        $phone = $request->get('phone');
        $code  = random_int(1000,9999);
        $data = [
            'phone'=>$phone,
            'code' =>$code,
            'type' =>1,
            'status'=>1,
        ];
        UserRegistCode::where([['phone',$phone],['type',1]])->update(['status'=>2]);
        UserRegistCode::create($data);
        $message="注册验证码是：".$code;
        $flag =$this->send_message($phone,$message);
        return $flag==100?
            ['status_code'=>"1",'message'=>"发送成功"]:
            ['status_code'=>"0",'message'=>"发送失败".$flag];
    }

    public function auth_code($phone,$code,$type=1)
    {
       $code =  UserRegistCode::where([['phone',$phone],['code',$code],['status',1],['type',$type]])->first();
       if($code){
           $code->status=2;
           $code->save();
       }
       return $code?true:false;
    }

    public function reset_code(Request $request)
    {
        $phone =$request->get('phone');
        if($phone){
            $code  = random_int(1000,9999);
            $data = [
                'phone'=>$phone,
                'code' =>$code,
                'type' =>2,
                'status'=>1,
            ];
            UserRegistCode::where([['phone',$phone],['type',2]])->update(['status'=>2]);
            UserRegistCode::create($data);
            $message="修改密码验证码是：".$code;
            $flag =  $this->send_message($phone,$message);
            return $flag==100?
                ['status_code'=>"1",'message'=>"发送成功"]:
                ['status_code'=>"0",'message'=>"发送失败"];
        }else{
            return ['status_code'=>'200','message'=>"phone is required"];
        }
    }

    public function reset_password(Request $request)
    {
        $flag = $this->auth_code($request->get('phone'),$request->get('code'),2);
        if($flag){
            $user = User::where('phone',$request->get('phone'))
                ->update(['password'=>bcrypt($request->get('password'))]);
            return $user?
                ['status_code'=>"1",'message'=>"修改成功"]:
                ['status_code'=>"0",'message'=>"修改失败"];
        }
        return ['status_code'=>0,'message'=>"验证码错误"];
    }

    //修改用户信息
    public function edit(Request $request)
    {
        $user = $request->get('user');
        if($request->has('nickname')){
            $user->nickname = $request->get('nickname');
        }
        if ($request->hasFile('avatar')) {
            $avatar = $this->upload_img($request->avatar,'upload/user_avatar',1);
            if($avatar['status']==1){
                $user->avatar = $avatar['img']['pic_id'];
            }else{
                return ['status_code'=>0,'message'=>$avatar['message']];
            }
        }
        $user->save();
        return ['status_code'=>1,'message'=>"修改成功"];
    }

    //修改用户信息
    public function h5_edit(Request $request)
    {
        $user = $request->get('user');
        if($request->has('nickname')){
            $user->nickname = $request->get('nickname');
        }
        if ($request->hasFile('avatar')) {
            $avatar = $this->upload_img($request->avatar,'upload/user_avatar',1);
            if($avatar['status']==1){
                $user->avatar = $avatar['img']['pic_id'];
            }else{
                return ['status_code'=>0,'message'=>$avatar['message']];
            }
        }
        $user->save();
        return redirect('http://egui.gebixianxiang.com/Eh5/html/center.html');
    }

    public function intergral_detail(Request $request)
    {
        $user = $request->get('user');
        $intergaral = IntegralDetail::where('user_id',$user->user_id)->orderBy('created_at','desc')->get();
        return ['status_code'=>1,'data'=>$intergaral];
    }

    public function share(Request $request)
    {
        $user = $request->get('user');
        if(time()-strtotime($user->share_time)>86400){
            $share = Config::where('name','SHARE')->first();
            $user->user_integral+=$share->value;
            $user->share_time = date('Y-m-d H:i:s',time());
            $user->save();
            $integral = new IntegralDetail;
            $integral->user_id=$user->user_id;
            $integral->type=1;
            $integral->value=$share->value;
            $integral->save();
            return ['status_code'=>1,'message'=>"分享成功，积分+".$share->value];
        }else{
            return ['status_code'=>0,'message'=>"距上次分享得积分时间小于24小时"];
        }
    }
}
